package com.restaurant.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.restaurant.domain.Menu;
import com.restaurant.domain.Menuspicy;
import com.restaurant.domain.Spicymaster;
import com.restaurant.service.MenuService;
import com.restaurant.service.MenuspicyService;
import com.restaurant.service.SpicymasterService;


@Component
@RequestMapping("/action/menuspicy.do")
@SessionAttributes("menu")
public class MenuSpicyFormController {

	private final String loadAllMenu = "from Menu where isdeleted = false";
	
	@Autowired
	private MenuService objMenuService;
	
	@Autowired
	private MenuspicyService objMenuspicyService;
	
	@Autowired
	private SpicymasterService objSpicymasterService;
	
	
	@RequestMapping(method=RequestMethod.GET)
	public String menuSpicyMasterGETRequest(ModelMap map){
		ArrayList<Menu> listMenus = null;
		Menu menu = new Menu();
		try {
			listMenus = (ArrayList<Menu>) objMenuService.loadByHql(loadAllMenu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("listMenus", listMenus);
		map.addAttribute("menu", menu);
		return "MenuSpicy";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String menuSpicyMasterPOSTRequest(ModelMap map, @ModelAttribute ArrayList<Menu> listMenus, @ModelAttribute Menu menu, BindingResult result, HttpServletRequest request, HttpServletResponse response){
		String action = request.getParameter("action");
		
		if(action.compareTo("onselect")==0){
			if(menu.getMenuID()!=0)
				map = onSelectMenu(map, menu, request, response);
			else
				return "redirect:menuspicy.do";
		}else if(action.compareTo("edit")==0){
			map = onAddMenuSpicy(map, menu, request, response);
		}else if(action.compareTo("del")==0){
			map = onDeleteMenuSpicy(map, menu, request, response);
		}
		
		try {
			listMenus = (ArrayList<Menu>) objMenuService.loadByHql(loadAllMenu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("listMenus", listMenus);
		return "MenuSpicy";
	}
	
	private ModelMap onSelectMenu(ModelMap map, Menu menu, HttpServletRequest request, HttpServletResponse response){
		String menuID = menu.getMenuID().toString();		
		
		String queryLoadSpicyMasterForMenuID = "from Menuspicy where menu.menuID = " + menuID;
		
		ArrayList<Menuspicy> listMenuspicy = null;
		try {
			listMenuspicy = (ArrayList<Menuspicy>) objMenuspicyService.loadByHql(queryLoadSpicyMasterForMenuID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		StringBuffer spicymasterIDs = new StringBuffer("(");
		for(int i=0;i<listMenuspicy.size();i++){
			spicymasterIDs.append(listMenuspicy.get(i).getSpicymaster().getSpicyID());
			spicymasterIDs.append(",");
		}
		if(spicymasterIDs.lastIndexOf(",")>0)
			spicymasterIDs.deleteCharAt(spicymasterIDs.lastIndexOf(","));
		else
			spicymasterIDs.append("-1");
		spicymasterIDs.append(")");
		
		String queryNotMenuSpicy = "from Spicymaster where isdeleted = false and spicyID not in " + spicymasterIDs;
		
		ArrayList<Spicymaster> listNotMenuSpicys = null;
		
		try {
			listNotMenuSpicys = (ArrayList<Spicymaster>) objSpicymasterService.loadByHql(queryNotMenuSpicy);
		} catch (Exception e) {
			e.printStackTrace();	
		}
				
		map.addAttribute("listMenuspicy", listMenuspicy);
		map.addAttribute("listNotMenuSpicys", listNotMenuSpicys);
		map.addAttribute("menu", menu);
		
		return map;
	}
	
	private ModelMap onAddMenuSpicy (ModelMap map, Menu menu, HttpServletRequest request, HttpServletResponse response){
		Long menuID = menu.getMenuID();
		
		StringBuffer ids = new StringBuffer(request.getParameter("ids"));
		ids.deleteCharAt(ids.lastIndexOf(","));
		ids.insert(0, "(");
		ids.insert(ids.length(), ")");
		
		ArrayList<Spicymaster> listSpicymasters = null;
		try {
			listSpicymasters = (ArrayList<Spicymaster>) objSpicymasterService.loadByHql("from Spicymaster where spicyID in " + ids.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Menuspicy menuspicy = null;
		
		for (Spicymaster spicymaster : listSpicymasters) {
			menuspicy = new Menuspicy();
			menuspicy.setIsdeleted(false);
			menuspicy.setSpicymaster(spicymaster);
			try {
				menuspicy.setMenu(objMenuService.loadByPrimaryKey(menuID));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				objMenuspicyService.addOrModify(menuspicy);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return onSelectMenu(map, menu, request, response);
	}
	
	private ModelMap onDeleteMenuSpicy(ModelMap map, Menu menu, HttpServletRequest request, HttpServletResponse response){
		
		StringBuffer ids = new StringBuffer(request.getParameter("ids"));
		ids.deleteCharAt(ids.lastIndexOf(","));
		ids.insert(0, "(");
		ids.insert(ids.length(), ")");
		
		ArrayList<Menuspicy> listMenuspicies = null;
		
		try {
			listMenuspicies = (ArrayList<Menuspicy>) objMenuspicyService.loadByHql("from Menuspicy where menuspicyID in " + ids);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		for (Menuspicy menuspicy : listMenuspicies) {
			try {
				objMenuspicyService.deleteRecord(menuspicy);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return onSelectMenu(map, menu, request, response);
	}
}
